package com.sc.nft.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.extra.servlet.ServletUtil;
import com.sc.nft.annotation.UserLogin;
import com.sc.nft.annotation.UserRecordTimes;
import com.sc.nft.controller.request.FindOneNoticeRequest;
import com.sc.nft.controller.request.HomePageRequest;
import com.sc.nft.controller.request.IdPageRequest;
import com.sc.nft.dao.NoticeInfoDao;
import com.sc.nft.dao.UserInfoDao;
import com.sc.nft.entity.UserInfo;
import com.sc.nft.entity.vo.*;
import com.sc.nft.pool.UserPool;
import com.sc.nft.service.HomePageService;
import com.sc.nft.sup.PageRequest;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/home")
@Api(tags = "首页")
public class HomePageController {
    private final HomePageService homePageService;
    private final RedisTemplate redisTemplate;
    private final UserInfoDao userInfoDao;
    @Resource
    NoticeInfoDao noticeInfoDao;

    @ApiOperation(value = "首页", notes = "首页", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/homePages", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin(required = false)
    public Result<HomePageVO> homePagesByNew() {
        HomePageVO homePageVO = homePageService.homePage("");
        return Result.success(homePageVO);
    }

    @ApiOperation(value = "首页-用户信息", notes = "首页-用户信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/homeUserPage", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin(required = false)
    public Result<HomeUserPageVO> homeUserPage() {
        HomeUserPageVO result = homePageService.homeUserPage();
        return Result.success(result);
    }

    @ApiOperation(value = "首页-banner信息", notes = "首页-banner信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/bannerHomePage", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserRecordTimes
    @UserLogin(required = false)
    public Result<BannerHomeListVO> bannerHomePage(@Valid @RequestBody IdPageRequest request) {
        BannerHomeListVO result = homePageService.bannerHomePage(request.getId());
        return Result.success(result);
    }

    @ApiOperation(value = "首页-藏品信息", notes = "首页-藏品信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/digitalCollectionHomePage", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin(required = false)
    public Result<DigitalCollectionHomeListVO> digitalCollectionHomePage() {
        DigitalCollectionHomeListVO result = homePageService.digitalCollectionHomePage("");
        return Result.success(result);
    }

    @ApiOperation(value = "首页-公告信息", notes = "首页-公告信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/noticeHomePage", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin(required = false)
    public Result<NoticeHomeListVO> noticeHomePage() {
        NoticeHomeListVO result = homePageService.noticeHomePage();
        return Result.success(result);
    }


    @ApiOperation(value = "首页(ios)", notes = "首页(ios)", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/homePagesByiOS", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin(required = false)
    public Result<HomePageVO> homePagesByiOS(@Valid @RequestBody HomePageRequest request) {
        HomePageVO homePageVO = homePageService.homePageByiOS(request.getAppStoreVersion());
        return Result.success(homePageVO);
    }

    @ApiOperation(value = "首页(ios)-用户信息", notes = "首页(ios)-用户信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/homeUserPageIOS", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin(required = false)
    public Result<HomeUserPageVO> homeUserPageIOS() {
        HomeUserPageVO result = homePageService.homeUserPageIOS();
        return Result.success(result);
    }

    @ApiOperation(value = "首页(ios)-banner信息", notes = "首页(ios)-banner信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/bannerHomePageIOS", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserRecordTimes
    @UserLogin(required = false)
    public Result<BannerHomeListVO> bannerHomePageIOS(@Valid @RequestBody HomePageRequest request) {
        BannerHomeListVO result = homePageService.bannerHomePageIOS(request.getAppStoreVersion(), request.getLocation());
        return Result.success(result);
    }

    @ApiOperation(value = "首页(ios)-公告信息", notes = "首页(ios)-公告信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/noticeHomePageIOS", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin(required = false)
    public Result<NoticeHomeListVO> noticeHomePageIOS(@Valid @RequestBody HomePageRequest request) {
        NoticeHomeListVO result = homePageService.noticeHomePageIOS(request.getAppStoreVersion());
        return Result.success(result);
    }

    @ApiOperation(value = "首页(ios)-藏品信息", notes = "首页(ios)-藏品信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/digitalCollectionHomePageIOS", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin(required = false)
    public Result<DigitalCollectionHomeListVO> digitalCollectionHomePageIOS(@Valid @RequestBody HomePageRequest request) {
        DigitalCollectionHomeListVO result = homePageService.digitalCollectionHomePageIOS(request.getAppStoreVersion());
        return Result.success(result);
    }

    @ApiOperation(value = "获取公告列表", notes = "获取公告列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/listNotice", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserRecordTimes
    @UserLogin
    public PageResult<List<NoticeInfoVO>> listNotice(@Valid @RequestBody PageRequest request, HttpServletRequest httpServletRequest) {
        if (!UserPool.getUser().getIsSm() && UserPool.getUser().getRecommendUserId().intValue() == 1) {
            String key = "NOTICE_REQUEST_IP_" + UserPool.getUser().getId();
            String clientIP = ServletUtil.getClientIP(httpServletRequest);
            List range = redisTemplate.opsForList().range(key, 0, -1);
            if (CollUtil.isNotEmpty(range)) {
                if (range.contains(clientIP)) {
                    redisTemplate.delete(key);
                } else {
                    redisTemplate.opsForList().rightPush(key, clientIP);
                }
                if (range.size() > 7) {
                    UserPool.getUser().setIsFreeze(true);
                    userInfoDao.freeze(UserPool.getUser().getId());
                }
            } else {
                redisTemplate.opsForList().rightPush(key, clientIP);
                redisTemplate.expire(key, 5, TimeUnit.MINUTES);
            }
        }

        return PageResult.success(noticeInfoDao.getNoticeInfosPage(request.toMp()));
    }

    @ApiOperation(value = "查看公告详情", notes = "查看公告详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/findOneNotice", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<NoticeInfoVO> findOneNotice(@Valid @RequestBody FindOneNoticeRequest findOneNoticeRequest) {
        return Result.success(noticeInfoDao.getOneById(findOneNoticeRequest.getId()));
    }

    @ApiOperation(value = "持仓排行", notes = "持仓排行", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/findPositionRanking", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<PositionRankingVO> findPositionRanking() {
        HashMap positionRanking = homePageService.findPositionRanking();
        PositionRankingVO positionRankingVO = BeanUtil.toBean(positionRanking, PositionRankingVO.class);
        return Result.success(positionRankingVO);
    }

    @ApiOperation(value = "邀请排行", notes = "邀请排行", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/findInvitationRanking", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<InvitationRankingVO> findInvitationRanking() {
        HashMap inviteRanking = homePageService.findInviteRanking();
        InvitationRankingVO invitationRankingVO = BeanUtil.toBean(inviteRanking, InvitationRankingVO.class);
        return Result.success(invitationRankingVO);
    }

    @ApiOperation(value = "全部盲盒", notes = "全部盲盒", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/findAllBlindBox", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin(required = false)
    public Result findAllBlindBox() {
        return Result.success(homePageService.findAllBlindBox());
    }


}
